﻿namespace LinkerMode;

/// <summary>
/// 链路器设计模式
/// </summary>
public class LinkerModeClass
{
    static async Task testMain()
    {
        var host = new LinkerWebHost();
        host.AddFilter(new Filter1());
        host.AddFilter(new Filter2());

        var context = new HttpContext("Get");

        var servlet = new HelloServlet();
         
        await host.ExecuteAsync(context,servlet);

    }
}

public class HttpContext
{
    public string Method { get; set; }
    public HttpContext(string method)
    {
        Method = method;
    }
}
/// <summary>
/// 链路器
/// </summary>
public interface IChain
{
    Task NextAsync();
}
/// <summary>
/// 适配 Filter
/// </summary>
public class FilterChain: IChain
{
    private readonly IChain _chain;
    private readonly IFilter _filter;
    private readonly HttpContext _context;

    public FilterChain(IFilter filter, HttpContext context, IChain chain)
    {
        _context = context;
        _filter = filter;
        _chain = chain;
    }
    public async Task NextAsync() 
    {
        await _filter.InvokeAsync(_context, _chain);
    }

}

/// <summary>
/// 适配 Servlet
/// </summary>
public class ServletChain : IChain
{ 
    private readonly IServlet _servlet;
    private readonly HttpContext _context;

    public ServletChain(IServlet servlet, HttpContext context)
    {
        _context = context;
        _servlet = servlet; 
    }
    public async Task NextAsync()
    {
        await _servlet.InvokeAsync(_context);
    }

}


public interface IFilter
{
    Task InvokeAsync(HttpContext context,IChain chain);
}
public class Filter1 : IFilter
{
    public async Task InvokeAsync(HttpContext context, IChain chain)
    {
        Console.WriteLine("Linker--111111");
        await chain.NextAsync();
        Console.WriteLine("Linker--222222");
    }
}
public class Filter2 : IFilter
{
    public async Task InvokeAsync(HttpContext context, IChain chain)
    {
        Console.WriteLine("Linker--333333");
        await chain.NextAsync();
        Console.WriteLine("Linker--444444");
    }
}


/// <summary>
/// 模板方法 实现
/// </summary>
public class HelloServlet : httpServlet
{
    public override Task GetAsync(HttpContext context)
    {
        Console.WriteLine("Get:hello world");
        return Task.CompletedTask;
    }
}

public interface IServlet
{
    Task InvokeAsync(HttpContext context);
}


/// <summary>
/// 模板方法设计模式   模板方法
/// </summary>
public abstract class httpServlet : IServlet
{
    public async Task InvokeAsync(HttpContext context)
    {
        if (context.Method == "Get")
        {
            await GetAsync(context);
        }
        else if (context.Method == "Post")
        {
            await PostAsync(context);
        }
    }
    //public abstract Task GetAsync(HttpContext context);
    //public abstract Task PostAsync(HttpContext context);

    public virtual Task GetAsync(HttpContext context)
    {
        return Task.CompletedTask;
    }
    public virtual Task PostAsync(HttpContext context)
    {
        return Task.CompletedTask;
    }
}
